package com.szs.controller;


import com.szs.pojo.Emp;
import com.szs.pojo.EmpQueryParam;
import com.szs.pojo.PageResult;
import com.szs.pojo.Result;
import com.szs.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;


/*
* 员工管理Controller
* */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;
    /*
    *分页查询 方法一
     */
    /*@GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                        String name, Integer gender,
                       @DateTimeFormat(pattern = "yyyy-mm-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-mm-dd") LocalDate end){
        log.info("page:{},pageSize:{},name:{},gender:{},begin:{},end:{}",page,pageSize,name,gender,begin,end);
        PageResult<Emp> pageResult =empService.page(page,pageSize,name,gender,begin,end);
        return Result.success(pageResult);
    }*/

    /*
     *分页查询 方法二，优化版，定义一个EmpQueryParam对象，将传进来的形参封装到该对象当中
     */
    @GetMapping
    public Result page(EmpQueryParam empQueryParam){
        log.info("分页查询:{}",empQueryParam);
        PageResult<Emp> pageResult =empService.page(empQueryParam);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("查询id员工信息:{}",id);
        Emp emp = empService.getInfo(id);
        return Result.success(emp);
    }

    @GetMapping("/list")
    public Result list(){
        log.info("查询所有员工信息");
        List<Emp> empList = empService.findAll();
        return Result.success(empList);
    }

    /*
    * 新增员工操作
    * */
    //@RequestBody 接收json格式的请求参数
    @PostMapping
    public Result save(@RequestBody Emp emp){
        log.info("新增员工：{}",emp);
        empService.save(emp);
        return Result.success();
    }


    /*
    * 删除员工：数组
    * */
    /*@DeleteMapping()
    public Result delete(Integer ids){
        log.info("删除员工：{}",ids);
        return Result.success();
    }*/
    /*
     * 删除员工：集合List (推荐)
     * */
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        log.info("删除员工：{}",ids);
        empService.delete(ids);
        return Result.success();
    }



    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工：{}",emp);
        empService.update(emp);
        return Result.success();
    }



}
